package com.android.SchoolManagerSystem.action;

import java.net.HttpURLConnection;
import java.util.HashMap;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.android.SchoolManagerSystem.CommonInfo;
import com.android.SchoolManagerSystem.data.Transcript;
import com.android.SchoolManagerSystem.data.Userinfo;
import com.android.SchoolManagerSystem.net.HttpRequester;
import com.android.SchoolManagerSystem.net.HttpResponser;

public class GetTranScript {
	private Userinfo curinfo = null;
	private Transcript tmpScript = null;
	private String strSemester = null;
	private boolean berrorcode = true;
	private String stryear = null;

	public GetTranScript(Userinfo curuser, String strSemesterin,
			String stryearin) {
		curinfo = curuser;
		strSemester = strSemesterin;
		stryear = stryearin;
	}

	public boolean go() {
		if (curinfo.MyScore.size() != 0)
			curinfo.MyScore.clear();
		// clear list if there are some queried items
		HashMap<String, String> propertys = new HashMap<String, String>();
		propertys.put("Set-Cookie", "ONE_VER=1_0;sid=10006");
		propertys.put("H-Product", "client");

		String strrequetstXMl = generatexmlstring();
		HttpRequester loginrequest = new HttpRequester();
		HttpResponser loginresponse = loginrequest.sendPost(
				CommonInfo.strGetTranscripturi, propertys, strrequetstXMl);
		if (loginresponse == null
				|| loginresponse.getCode() != HttpURLConnection.HTTP_OK)
			return false;
		else {
			if (decodelogresponce(loginresponse))
				return true;
			else
				return false;
		}
	}

	private String generatexmlstring() {
		String strlogreq = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<queryallscore apiversion=\"1.0\"><userid>"
				+ curinfo.struserid + "</userid><year>" + stryear
				+ "</year><Semester>" + strSemester
				+ "</Semester></queryallscore>";
		return strlogreq;
	}

	private boolean decodelogresponce(HttpResponser repsonse) {
		RootElement root = new RootElement("queryallscorer");

		root.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
			}
		});
		root.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
			}
		});

		Element e = root.getChild("ccula");

		e.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				tmpScript = new Transcript();
				tmpScript.strUserid = curinfo.struserid;
				tmpScript.strSemester = strSemester;
				berrorcode = false;
			}
		});

		e.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
				curinfo.MyScore.add(tmpScript);
			};
		});

		e.getChild("culaid").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							tmpScript.CCula.strCurriculaName = body;
						}
					}
				});
		e.getChild("score").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							tmpScript.strMark = body;
						}
					}
				});
		e.getChild("transcript").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							tmpScript.strCredit = body;
						}
					}
				});

		e.getChild("errorcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						int ires = body.compareTo("0");
						if(ires != 0)
							berrorcode = true;
					}
				});

		try {
			Xml.parse(repsonse.getContent(), root.getContentHandler());
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		if (berrorcode)
			return false;
		else
			return true;
	}
}
